// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-28

package detail

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/lib/authorizer"
)

// Logic
// 渔票详情.
type Logic struct {
	auth     *authorizer.Authentication
	request  *Request
	response *Response

	arcade *models.Arcades
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.auth = authorizer.New().Get(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)
	if err = db.Transaction(ctx,
		o.RunCheck,
		o.RunArcade,
		o.RunSeat,
		o.RunSession,
		o.RunTicket,
		o.RunPond,
	); err != nil {
		return res.WithError(err)
	}
	return res.With(o.response)
}

// RunArcade
// 钓场信息.
func (o *Logic) RunArcade(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.Arcades
	)

	// 1. 忽略钓场.
	if o.response.ArcadeId.IsUnbound() {
		return
	}

	// 2. 读取钓场.
	if model, has, err = services.NewArcadesService(sess).Dao.GetById(ctx, o.response.ArcadeId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrArcadeNotFound
		return
	}

	// 3. 绑定钓场.
	o.response.WithArcade(model)
	return
}

// RunCheck
// 校验场次.
func (o *Logic) RunCheck(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.TicketRecord
	)

	// 1. 读取场次.
	if model, has, err = services.NewTicketRecordService(sess).Dao.GetById(ctx, o.request.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has || model.UserId != o.auth.UserId {
		err = errs.ErrTicketRecordNotFound
		return
	}

	// 2. 绑定场次.
	o.response.With(model)
	return
}

// RunPond
// 鱼塘信息.
func (o *Logic) RunPond(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.ArcadePond
	)

	// 1. 忽略鱼塘.
	if o.response.Session == nil || o.response.Session.Ticket == nil || o.response.Session.Ticket.PondId <= 0 {
		return
	}

	// 2. 读取鱼塘.
	if model, has, err = services.NewArcadePondService(sess).Dao.GetById(ctx, o.response.Session.Ticket.PondId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrArcadePondNotFound
		return
	}

	// 3. 绑定鱼塘.
	o.response.Session.Ticket.WithPond(model)
	return
}

// RunSeat
// 钓位信息.
func (o *Logic) RunSeat(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.ArcadePondSeat
	)

	// 1. 忽略钓位.
	if o.response.SeatId <= 0 {
		return
	}

	// 2. 读取钓位.
	if model, has, err = services.NewArcadePondSeatService(sess).Dao.GetById(ctx, o.response.SeatId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrArcadeSeatNotFound
		return
	}

	// 3. 遍历钓位.
	o.response.WithSeat(model)
	return
}

// RunSession
// 场次信息.
func (o *Logic) RunSession(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.TicketSession
	)

	// 1. 忽略场次.
	if o.response.SessionId <= 0 {
		return
	}

	// 2. 读取场次.
	if model, has, err = services.NewTicketSessionService(sess).Dao.GetById(ctx, o.response.SessionId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrTicketSessionNotFound
		return
	}

	// 3. 绑定场次.
	o.response.WithSession(model)
	return
}

// RunTicket
// 渔票信息.
func (o *Logic) RunTicket(ctx context.Context, sess *db.Session) (err error) {
	var (
		has   bool
		model *models.Ticket
	)

	if o.response.Session == nil || o.response.Session.TicketId <= 0 {
		return
	}

	// 1. 渔票列表.
	if model, has, err = services.NewTicketService(sess).Dao.GetById(ctx, o.response.Session.TicketId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrTicketNotFound
		return
	}

	// 3. 绑定鱼塘.
	o.response.Session.WithTicket(model)
	return
}
